package com.im.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.im.service.common.smo.ICommonSMO;
import com.im.service.dto.Orgarization;
import com.im.service.orgarization.smo.IOrgarizationQuerySmo;
import com.im.util.JsonUtil;
import com.im.util.ReturnsUtil;

import net.sf.json.JSONObject;

@RestController
@RequestMapping("/commonService")
public class CommonService {

    private static final Logger logger = LogManager.getLogger(CommonService.class);

    @Autowired
    ICommonSMO commonSMO;

    @Autowired
    private IOrgarizationQuerySmo orgarizationQuerySmo;

    /**
     * 根据表名和列名查询字典表
     * @param
     * @return
     */
    @PostMapping(value = "/queryTabColParam")
    public @ResponseBody String queryTabColParam(@RequestBody String inJson) {
        if (logger.isInfoEnabled()) {
            logger.info("请求入参json:{}", inJson);
        }
        try {
        	Map<String, Object> param = JsonUtil.getMap(inJson);
            return ReturnsUtil.returnSuccess(commonSMO.queryTabColParam(param)).toString();
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error(e.getStackTrace());
            }
            return ReturnsUtil.returnFailure(e.getMessage());
        }
    }

    @PostMapping(value = "/queryOrg")
    public @ResponseBody String queryOrg(Orgarization orgarization) throws Exception {
        List<Orgarization> list=orgarizationQuerySmo.queryAllOrg(orgarization);
        logger.info("hhhhhhhhhhhh"+list);
       return ReturnsUtil.returnSuccess(list).toString();
    }

    /**
     * 查询常量配置信息
     * @param jsonString
     * @return
     */
    @PostMapping("/queryBusiConstByCond")
    public @ResponseBody String queryBusiConstByCond(@RequestBody JSONObject jsonString) {
        try {
            return ReturnsUtil.returnSuccess(commonSMO.queryBusiConstByCond(jsonString)).toString();
        } catch (Exception e) {
            logger.error("查询常量配置信息异常，{}",e);
            return ReturnsUtil.returnException("查询常量值配置信息异常，{}",e).toString();
        }
    }
    /**
     * 查询常量值配置信息
     * @param jsonString
     * @return
     */
    @PostMapping("/queryBusiConstValByCond")
    public @ResponseBody String queryBusiConstValByCond(@RequestBody JSONObject jsonString) {
        try {
            return ReturnsUtil.returnSuccess(commonSMO.queryBusiConstValByCond(jsonString)).toString();
        } catch (Exception e) {
            logger.error("查询常量值配置信息异常，{}",e);
            return ReturnsUtil.returnException("查询常量值配置信息异常，{}",e).toString();
        }
    }

    /**
     * 保存或更新角色信息
     * @param jsonString
     * @return
     */
    @RequestMapping("/saveOrUpdateBusiParam")
    @ResponseBody
    public String saveOrUpdateBusiParam(@RequestBody JSONObject jsonString) {
        try {
            commonSMO.saveOrUpdateBusiConst(jsonString);
            return ReturnsUtil.returnSuccess("{}").toString();
        } catch (Exception e) {
            logger.error("常量信息保存或更新查询异常，{}",e);
            return ReturnsUtil.returnException("常量信息保存或更新查询异常，{}",e).toString();
        }
    }
    /**
     * 保存或更新文档模板常量配置
     * @param jsonString
     * @return
     */
    @RequestMapping("/saveOrUpdateFileTemp")
    @ResponseBody
    public String saveOrUpdateFileTemp(@RequestBody JSONObject jsonString) {
        try {
            commonSMO.saveOrUpdateFileTemp(jsonString);
            return ReturnsUtil.returnSuccess("{}").toString();
        } catch (Exception e) {
            logger.error("常量信息保存或更新查询异常，{}",e);
            return ReturnsUtil.returnException("常量信息保存或更新查询异常，{}",e).toString();
        }
    }

    @RequestMapping("/getSeq")
    @ResponseBody
    public String getSeq(@RequestBody JSONObject jsonString) {
        try {
            String type = jsonString.getString("tableName");
            String seq = commonSMO.querySeq(type);
            JSONObject jo = new JSONObject();
            jo.element("seq",seq);
            return ReturnsUtil.returnSuccess(jo).toString();
        } catch (Exception e) {
            logger.error("取序列异常，{}",e);
            return ReturnsUtil.returnException("取序列异常，{}",e).toString();
        }
    }

	@RequestMapping("/export")
	@ResponseBody
	public void export(HttpServletRequest request, HttpServletResponse response) {
		try {
			commonSMO.getExportExcel(request, response);
			return;
		} catch (Exception e) {
			logger.error("导出数据异常，{}", e);
			return;
		}
	}


    @RequestMapping("/getSeqForRule")
    @ResponseBody
    public String getSeqForRule(@RequestBody JSONObject jsonString) {
        try {
            String seqType = jsonString.getString("seqType");
            String seqArg = jsonString.getString("seqArg");
            String seqVal = commonSMO.querySeqForRule(seqType,seqArg);
            JSONObject jo = new JSONObject();
            jo.element("seq",seqVal);
            return ReturnsUtil.returnSuccess(jo).toString();
        } catch (Exception e) {
            logger.error("取序列异常，{}",e);
            return ReturnsUtil.returnException("取序列异常，{}",e).toString();
        }
    }

    @RequestMapping("/importExcel")
    @ResponseBody
    public String importExcel(HttpServletRequest request, HttpServletResponse response) {
        try {
            return  commonSMO.saveOrUpdateExcel(request, response);
        } catch (Exception e) {
            logger.error("导出数据异常，{}", e);
            return ReturnsUtil.returnException("excel模板导入异常，{}",e).toString();
        }
    }

}
